<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php学习笔记[041]</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="show"><span>0041 多关键字模糊搜索和高亮显示</span></div>
<?php include 'include/top.php';?>
<div class="text">
<b>like</b>
<strong>like</strong>在sql语句中通常和<strong>%,_</strong>一起匹配<br />
<strong>%</strong>匹配0个或多个，<strong>_</strong>匹配单个字符。<br />
我们列举3个sql说明like的用法<br />
"select * from `study`.`study011` where title like '0%'":匹配起始为0的字段<br />
"select * from `study`.`study011` where title like '%0'":匹配末尾为0的字段<br />
"select * from `study`.`study011` where title like '%0%'":匹配含有0的字段<br />
思路：
1.我们先吧用户输入的关键字用[ ],[+]分割成数组<br />
2.用sql语句查出数据库内有该内容的值<br />
3.用正则替换关键字加粗加颜色<br />
4.输出我们需要的结果<br />
我们先讲解两个关键字的搜索来讲解模糊和高亮的原理，看下面的例子：<br />
<div class="code">
&lt;?php<br />
include('include/mysql_class.php');<br />
$db=new mysql('localhost','root','','study','utf8');<br />
if(isset($_GET['key']))<br />
{<br />
$key=$_GET['key'];<br />
$key=explode(' ',$key);//分割为数组<br />
if (@$key[1]!='')//避免没有第二个关键词<br />
{<br />
//print_r($key);<br />
echo @$sql=&quot;select * from `study011` where CONTENT like '%$key[1]%' or CONTENT like '%$key[0]%'&quot;;<br />
echo '&lt;hr&gt;';<br />
$query=$db-&gt;query($sql);<br />
while($rs=$db-&gt;arr($query))<br />
{<br />
$rs['CONTENT']=preg_replace(&quot;/($key[0])/&quot;,&quot;&lt;font color='#c00'&gt;\\1&lt;/font&gt;&quot;,$rs['CONTENT']);<br />
echo $rs['CONTENT']=preg_replace(&quot;/($key[1])/&quot;,&quot;&lt;font color='c900'&gt;\\1&lt;/font&gt;&quot;,$rs['CONTENT']);<br />
echo '&lt;br /&gt;';<br />
} <br />
}<br />
else{echo '输入两个值用空格分开';}<br />
}<br />
?&gt;<br />
&lt;form name=&quot;form1&quot; action=&quot;study041.php&quot; method=&quot;get&quot;&gt;<br />
&lt;input name=&quot;key&quot; size=&quot;25&quot;&gt;<br />
&lt;input name=&quot;sub&quot; type=&quot;submit&quot; value=&quot;查询&quot;&gt;<br />
&lt;/form&gt;</div>
我们来测试这段代码：
<div class="effect">
<?php
include('include/mysql_class.php');
$db=new mysql('localhost','zt_f','yl11fc0','zt','utf8');
if(isset($_GET['key']))
	{
	$key=$_GET['key'];
	$key=explode(' ',$key);//分割为数组
	if (@$key[1]!='')//避免没有第二个关键词
		{
		//print_r($key);
		echo @$sql="select * from `study011` where CONTENT like '%$key[1]%' or CONTENT like '%$key[0]%'";
		echo '<hr>';
		$query=$db->query($sql);
		while($rs=$db->arr($query))
			{
			$rs['CONTENT']=preg_replace("/($key[0])/","<font color='#c00'>\\1</font>",$rs['CONTENT']);
			echo $rs['CONTENT']=preg_replace("/($key[1])/","<font color='c900'>\\1</font>",$rs['CONTENT']);
			echo '<br />';
			}	
		}
	else{echo '输入两个值用空格分开';}
	}
?>
<form name="form1" action="study041.php" method="get">
<input name="key" size="25">
<input name="sub" type="submit" value="查询">
</form>
</div>
</div>




</body>
</html>
